package com.maktoub.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maktoub.common.model.entity.InterfaceInfo;
import com.maktoub.common.model.entity.User;
import com.maktoub.common.model.entity.UserInterfaceInfo;
import com.maktoub.project.annotation.AuthCheck;
import com.maktoub.project.common.BaseResponse;
import com.maktoub.project.common.ErrorCode;
import com.maktoub.project.common.ResultUtils;
import com.maktoub.project.exception.BusinessException;
import com.maktoub.project.mapper.InterfaceCollectInfoMapper;
import com.maktoub.project.mapper.UserInterfaceInfoMapper;
import com.maktoub.project.mapper.UserMapper;
import com.maktoub.project.model.vo.analysi.collect.InterfaceCollectNumVO;
import com.maktoub.project.model.vo.analysi.totalapply.InterfaceTotalApplyVO;
import com.maktoub.project.model.vo.analysi.applyRecord.InterfaceApplyRecordColum;
import com.maktoub.project.model.vo.analysi.applyRecord.InterfaceApplyRecordVO;
import com.maktoub.project.model.vo.analysi.methodcount.MethodCountVO;
import com.maktoub.project.model.vo.analysi.userregister.UserRegisterColum;
import com.maktoub.project.model.vo.analysi.userregister.UserregisterRecordVO;
import com.maktoub.project.service.InterfaceInfoService;
import com.maktoub.project.service.UserInterfaceInfoService;
import com.maktoub.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/analysis")
public class AnalysiController {
    @Autowired
    UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Autowired
    InterfaceCollectInfoMapper interfaceCollectInfoMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Autowired
    UserInterfaceInfoService userInterfaceInfoService;
    @Autowired
    InterfaceInfoService interfaceInfoService;

    @GetMapping("total_apply")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceTotalApplyVO>> getTotalApplyMes() {
        List<InterfaceTotalApplyVO> interfaceTotalApplyVO = userInterfaceInfoMapper.selectInterfaceTotalCount();
        Long sumTotalNum = userInterfaceInfoMapper.selectSumTotalNum();
        int size = interfaceTotalApplyVO.size();
        int min = Math.min(size, 5);
        List<InterfaceTotalApplyVO> interfaceTotalApplyVOSub = interfaceTotalApplyVO.subList(0, min);
        List<InterfaceTotalApplyVO> other = interfaceTotalApplyVO.subList(min, size);
        int otherTotal=0;
        for(InterfaceTotalApplyVO item:other){
            Integer totalNum = item.getTotalNum();
            otherTotal+=totalNum;
        }


        for (InterfaceTotalApplyVO item : interfaceTotalApplyVOSub) {
            Integer totalNum = item.getTotalNum();
            item.setPercent(totalNum / (sumTotalNum * 1.0));
        }
        InterfaceTotalApplyVO temp = new InterfaceTotalApplyVO();
        temp.setName("其他");
        temp.setTotalNum(otherTotal);
        temp.setPercent(otherTotal/(sumTotalNum*1.0));
        interfaceTotalApplyVOSub.add(temp);

        return ResultUtils.success(interfaceTotalApplyVOSub);
    }

    @GetMapping("/apply_record")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<InterfaceApplyRecordVO> getInterfaceApplyRecordByDate() {
        List<InterfaceApplyRecordColum> interfaceApplyRecordColums = userInterfaceInfoMapper.selectRecordsByDate();
        int totalNum=0;
        for(InterfaceApplyRecordColum item:interfaceApplyRecordColums){
            totalNum+=item.getRecordCount();
        }
        InterfaceApplyRecordVO interfaceApplyRecordVO = new InterfaceApplyRecordVO();
        interfaceApplyRecordVO.setInterfaceApplyRecordColum(interfaceApplyRecordColums);
        interfaceApplyRecordVO.setTotalRecord(totalNum);


        QueryWrapper<UserInterfaceInfo> userInterfaceInfoQueryWrapper = new QueryWrapper<>();
        LocalDate now = LocalDate.now();
        userInterfaceInfoQueryWrapper.apply("Date(create_time) = {0}",now);
        List<UserInterfaceInfo> list = userInterfaceInfoService.list(userInterfaceInfoQueryWrapper);
        int todayNum = list.size();
        interfaceApplyRecordVO.setTodayRecord(todayNum);
        return ResultUtils.success(interfaceApplyRecordVO);

    }


    @GetMapping("/apply_record/user_id")
    public BaseResponse<InterfaceApplyRecordVO> getInterfaceApplyRecordByDateForUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        Long id = loginUser.getId();
        List<InterfaceApplyRecordColum> interfaceApplyRecordColums = userInterfaceInfoMapper.selectRecordsByDateForUser(id);
        int totalNum=0;
        for(InterfaceApplyRecordColum item:interfaceApplyRecordColums){
            totalNum+=item.getRecordCount();
        }
        InterfaceApplyRecordVO interfaceApplyRecordVO = new InterfaceApplyRecordVO();
        interfaceApplyRecordVO.setInterfaceApplyRecordColum(interfaceApplyRecordColums);
        interfaceApplyRecordVO.setTotalRecord(totalNum);

        QueryWrapper<UserInterfaceInfo> userInterfaceInfoQueryWrapper = new QueryWrapper<>();
        LocalDate now = LocalDate.now();
        userInterfaceInfoQueryWrapper.apply("Date(create_time) = {0}",now);
        userInterfaceInfoQueryWrapper.eq("user_id",id);
        List<UserInterfaceInfo> list = userInterfaceInfoService.list(userInterfaceInfoQueryWrapper);
        int todayNum = list.size();
        interfaceApplyRecordVO.setTodayRecord(todayNum);
        return ResultUtils.success(interfaceApplyRecordVO);

    }


    @GetMapping("collect_num")
    public BaseResponse<List<InterfaceCollectNumVO>> getCollectNums() {
        List<InterfaceCollectNumVO>  interfaceCollectNumVO = interfaceCollectInfoMapper.selectCollctNum();
        return ResultUtils.success(interfaceCollectNumVO);
    }


    @GetMapping("/user_register_record")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<UserregisterRecordVO> getUserRegisterRecordByDate() {
        List<UserRegisterColum> userRegisterColums = userMapper.selectRegisterUserGroupByDate();
        int totalNum=0;
        for(UserRegisterColum item:userRegisterColums){
            totalNum+=item.getRecordCount();
        }
        UserregisterRecordVO userregisterRecordVO = new UserregisterRecordVO();
        userregisterRecordVO.setUserRegisterColums(userRegisterColums);
        userregisterRecordVO.setTotalRecord(totalNum);


        QueryWrapper<User> userInterfaceInfoQueryWrapper = new QueryWrapper<>();
        LocalDate now = LocalDate.now();
        userInterfaceInfoQueryWrapper.apply("Date(create_time) = {0}",now);
        List<User> list = userService.list(userInterfaceInfoQueryWrapper);
        int todayNum = list.size();
        userregisterRecordVO.setTodayRecord(todayNum);
        return ResultUtils.success(userregisterRecordVO);

    }
    @GetMapping("/interface_method_count")
    public BaseResponse<List<MethodCountVO>> getDiffInterfaceMethodCount() {
        QueryWrapper<InterfaceInfo> interfaceInfoQueryWrapper = new QueryWrapper<>();
        interfaceInfoQueryWrapper.select("method","count(*) as count")
                .groupBy("method");
        List<Map<String, Object>> maps = interfaceInfoService.listMaps(interfaceInfoQueryWrapper);
        List<MethodCountVO> collect = maps.stream().map(map -> {
            MethodCountVO methodCountVO = new MethodCountVO();
            methodCountVO.setMethod((String) map.get("method"));
            methodCountVO.setCount((Long) map.get("count"));
            return methodCountVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }


}
